package cn.yunhe.control;

import cn.yunhe.entity.Blank;
import cn.yunhe.entity.Topic;
import cn.yunhe.entity.User;
import cn.yunhe.server.BlankServer;
import cn.yunhe.server.TopicServer;
import cn.yunhe.util.JasonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/GetTopicInfoServlet")
public class GetTopicInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int bId = Integer.parseInt(request.getParameter("bId"));
        String title = request.getParameter("title");
        //新建板块对象和主贴对象
        Blank blank = new Blank();
        blank.setbId(bId);
        Topic topic = new Topic();
        topic.setTitle(title);
        //新建server对象
        TopicServer topicServer = new TopicServer();
        BlankServer blankServer = new BlankServer();
        List<Blank> blanks = blankServer.getAllBlank(); //获取全部板块对象
        List<Topic> list = topicServer.AccurateSelectTopicByTopic(topic, blank); //获取主贴对象
        for (Topic t: list) {
            Blank blank1 = t.getBlank();
            Blank blank2 = blankServer.getBlankById(blank1.getbId());
            t.setBlank(blank2);
        }

        if (list != null && list.size() > 0) {
            request.setAttribute("blank",blanks);
            request.setAttribute("bId", bId);
            request.setAttribute("title", title);
            request.setAttribute("topicList", list);
            request.getRequestDispatcher("changeTopic.jsp").forward(request, response);
        } else {
            request.setAttribute("result", "暂无结果");
            request.getRequestDispatcher("changeTopic.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
